﻿Imports Global_Visas_Business_Layer
Imports System.Data
Imports System.ComponentModel
Partial Public Class frmAdminChangeProduct

    Private Sub Btn_Cancel_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Btn_Cancel.Click
        Try
            Dim StrSQL As String, Rst As DataTable, StrMess As String

            If Me.Txt_CaseID.Text = String.Empty Then Exit Sub

            If Not IsNumeric(Me.Txt_CaseID.Text) Then
                Call MsgBox("Not a valid Case ID", , "Error")
                Exit Sub
            End If

            StrSQL = "SELECT L_Products.Product_Name, D_Person.Person_FName + ' ' + D_Person.Person_LName AS Candidate, D_Case.Case_companyID, D_Company.Company_Name, "
            StrSQL = StrSQL & " L_Consultant_Sale.Con_Name AS ConSale, L_Consultant_CW.Con_Name AS ConCW, L_CaseStages.CaseStage_Title, Case_StatusID"
            StrSQL = StrSQL & " FROM         D_Company RIGHT OUTER JOIN"
            StrSQL = StrSQL & " D_Person RIGHT OUTER JOIN"
            StrSQL = StrSQL & " L_Products RIGHT OUTER JOIN"
            StrSQL = StrSQL & " D_Case LEFT OUTER JOIN"
            StrSQL = StrSQL & " L_Consultant L_Consultant_CW ON D_Case.Case_ConCWID = L_Consultant_CW.Con_ID LEFT OUTER JOIN"
            StrSQL = StrSQL & " L_Consultant L_Consultant_Sale ON D_Case.Case_ConSaleID = L_Consultant_Sale.Con_ID LEFT OUTER JOIN"
            StrSQL = StrSQL & " L_CaseStages ON D_Case.Case_StatusID = L_CaseStages.CaseStage_number ON L_Products.Product_ID = D_Case.Case_ProductID ON"
            StrSQL = StrSQL & " D_Person.Person_ID = D_Case.Case_CandidateID ON D_Company.Company_ID = D_Case.Case_CompanyID"
            StrSQL = StrSQL & " WHERE (D_Case.Case_ID = " & Me.Txt_CaseID.Text & ")"

            Rst = CommonDataFunctions.GetSQLDatatable(StrSQL)

            If Rst.Rows.Count = 0 Then
                Rst.Dispose()
                Call MsgBox("Cannot find Case number " & Me.Txt_CaseID.Text & " please check you have the correct details", , "Error")
                Exit Sub
            End If

            If Rst.Rows(0)("Case_StatusID") < 100 Then
                Rst.Dispose()
                Me.Txt_CaseID.Text = String.Empty
                Call MsgBox("That case has not yet been initiated, please change the product through the case screen", , "Admin not needed")
                Exit Sub
            End If

            If Rst.Rows(0)("Case_StatusID") > 999 Then
                Rst.Dispose()
                Me.Txt_CaseID.Text = String.Empty
                Call MsgBox("That case has been closed, cannot change its product now", , "Error")
                Exit Sub
            End If

            Me.Txt_CurrentProduct.Text = Rst.Rows(0)("Product_Name")

            StrMess = "For " & Rst.Rows(0)("Candidate") & vbCrLf
            If Rst.Rows(0)("Case_companyID") <> 0 Then
                StrMess = StrMess & "Company no. " & Rst.Rows(0)("Case_CompanyID") & " (" & Rst.Rows(0)("Company_Name") & ")" & vbCrLf
            End If
            StrMess = StrMess & "Sold by " & Rst.Rows(0)("ConSale") & vbCrLf
            StrMess = StrMess & "Caseworker " & Rst.Rows(0)("ConCW") & vbCrLf
            StrMess = StrMess & "Status " & Rst.Rows(0)("Casestage_Title")

            Me.Txt_StatusID.Text = Rst.Rows(0)("Case_StatusID")

            If Rst.Rows(0)("Case_CompanyID") = 0 Then
                Me.Txt_CType.Text = 1
            Else
                Me.Txt_CType.Text = 2
            End If

            Rst.Dispose()

            Me.Lbl_Details.Content = StrMess
            Me.Txt_CurrentProduct.Visibility = Windows.Visibility.Visible

            Me.Combo_ProductType.Visibility = Windows.Visibility.Visible
            Me.Combo_Product.Visibility = Windows.Visibility.Visible
            'Me.Combo_Status.Visibility = Windows.Visibility.Visible
            Me.Btn_Clear.Visibility = Windows.Visibility.Visible
            Me.Btn_Change.Visibility = Windows.Visibility.Visible
            Me.Label1.Visibility = Windows.Visibility.Visible
            Me.Label11.Visibility = Windows.Visibility.Visible
            Me.Label2.Visibility = Windows.Visibility.Visible
            Me.Label8.Visibility = Windows.Visibility.Visible
            Me.ProductType_Label.Visibility = Windows.Visibility.Visible
            Me.SQL_ProductTypes()
            Me.Combo_ProductType.Focus()

            Me.Txt_CaseID.IsReadOnly = True
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminChangeProduct-Txt_CaseID_LostKeyboardFocus")
        End Try
    End Sub

    Private Sub Btn_Change_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Btn_Change.Click
        Try

            Dim StrSQL As String, IntCSD As Integer, Rst As DataTable, StrMess As String
            Dim IntProductID As Integer, IntProductType As Integer, LngCase As Long, IntOldProduct As Integer

            If Me.Combo_Product.SelectedIndex = -1 Then
                Call MsgBox("Please select the new product", , "Error")
                Exit Sub
            End If

            LngCase = Me.Txt_CaseID.Text
            IntProductID = Me.Combo_Product.SelectedValue

            StrSQL = "Select Case_ProductID from D_Case where Case_ID = " & LngCase
            Rst = CommonDataFunctions.GetSQLDatatable(StrSQL)
            If Rst.Rows.Count = 0 Then
                Rst.Dispose()
                Call MsgBox("Can't find case", , "Error")
                Exit Sub
            End If

            IntOldProduct = Rst.Rows(0)("Case_ProductID")
            Rst.Dispose()


            If Me.Combo_Status.Visibility = Windows.Visibility.Hidden Then
                'Get the case stage diagram from the new product
                StrSQL = "Select Product_Type, Product_StagediagramId from L_Products where product_id = " & IntProductID

                Rst = CommonDataFunctions.GetSQLDatatable(StrSQL)

                If Rst.Rows.Count = 0 Then
                    MsgBox("Error state Product not found")
                    Exit Sub
                End If

                IntProductType = Rst.Rows(0)("Product_Type")
                IntCSD = Rst.Rows(0)("Product_StageDiagramID")

                Rst.Dispose()

                'Do we have what we want?
                If IntCSD = 0 Then
                    'No, try again with the product type

                    StrSQL = "Select ProductType_StageDiagramID"
                    StrSQL = StrSQL & " FROM L_ProductTypes "
                    StrSQL = StrSQL & " WHERE producttype_id = " & IntProductType

                    Rst = CommonDataFunctions.GetSQLDatatable(StrSQL)

                    If Rst.Rows.Count > 0 Then
                        'Found a product type, use that

                        IntCSD = Rst.Rows(0)("Producttype_StageDiagramID")
                    End If

                    Rst.Dispose()
                End If

                If IntCSD = 0 Then
                    Call MsgBox("No case stage diagram available for that product, please speak to IT", , "Error")
                    Exit Sub
                End If

                'check if the case's current status exists in the new products CSD for this customer type

                StrSQL = "SELECT StageLink_ID "
                StrSQL = StrSQL & " FROM L_StageLinks"
                StrSQL = StrSQL & " WHERE  (L_StageLinks.StageLink_CustomerType IN (" & Me.Txt_CType.Text & ", 0)) "
                StrSQL = StrSQL & " AND (L_StageLinks.StageLink_DiagramID = " & IntCSD & ") "
                StrSQL = StrSQL & " AND (( L_StageLinks.StageLink_FromID = " & Me.Txt_StatusID.Text & ") "
                StrSQL = StrSQL & " OR (L_StageLinks.StageLink_ToID = " & Me.Txt_StatusID.Text & ")) "

                Rst = CommonDataFunctions.GetSQLDatatable(StrSQL)
                If Rst.Rows.Count = 0 Then
                    Rst.Dispose()
                    'Not there, populate the combo box so they can select a suitable stage

                    StrSQL = "SELECT L_CaseStages.CaseStage_Number, MAX(L_CaseStages.CaseStage_Title) AS Title"
                    StrSQL = StrSQL & " FROM L_StageLinks INNER JOIN"
                    StrSQL = StrSQL & " L_CaseStages ON L_StageLinks.StageLink_FromID = L_CaseStages.CaseStage_Number"
                    StrSQL = StrSQL & " WHERE (L_StageLinks.StageLink_CustomerType IN (" & Me.Txt_CType.Text & ", 0)) "
                    StrSQL = StrSQL & " AND (L_StageLinks.StageLink_DiagramID = " & IntCSD & ") "
                    StrSQL = StrSQL & " AND (L_CaseStages.CaseStage_Number BETWEEN 100 AND 999)"
                    StrSQL = StrSQL & " GROUP BY L_CaseStages.CaseStage_Number"
                    StrSQL = StrSQL & " ORDER BY L_CaseStages.CaseStage_Number"

                    Dim dataItems As DataTable = CommonDataFunctions.GetSQLDatatable(StrSQL)
                    With Me.Combo_Status
                        .ItemsSource = CType(dataItems, IListSource).GetList
                        .SelectedValuePath = "CaseStage_Number"
                        .DisplayMemberPath = "Title"
                    End With
                    'Call GPL(StrSQL, "ChangeProduct_StatusList", Form_FMA_ChangeProduct.Combo_Status)
                    Me.Combo_Status = Nothing
                    Me.Combo_Status.Visibility = Windows.Visibility.Visible
                    Me.Combo_Status.Focus()

                    Call MsgBox("This case's current status is not valid in the new products Stage diagram, please select a matching status from the list.", , "Check Status")

                    Exit Sub

                End If
                Rst.Dispose()
            Else
                If Me.Combo_Status.SelectedIndex = -1 Then
                    Call MsgBox("You need to select a matching status from Case Stages in the list", , "Error")
                    Me.Combo_Status.Focus()
                    Exit Sub
                End If

                StrMess = "Status set to " & Me.Combo_Status.Text

            End If

            StrMess = "Product changed from " & Me.Txt_CurrentProduct.Text & " to " & Me.Combo_Product.Text & " with the Admin tool" & vbCrLf & vbCrLf & StrMess

            StrSQL = "Update D_Case set Case_ProductID = " & Me.Combo_Product.SelectedValue

            If Me.Combo_Status.Visibility = Windows.Visibility.Visible Then
                StrSQL = StrSQL & " , Case_StatusID = " & Me.Combo_Status.SelectedValue
            End If

            StrSQL = StrSQL & " Where Case_ID = " & LngCase

            If CommonDataFunctions.ExecuteSQL(StrSQL) = False Then Exit Sub

            Call Admin.Gblfunc_Add_CaseAudit(LngCase, 1, CLng(IntOldProduct), Me.Combo_Product.SelectedValue)
            'Call GblFunc_Add_Event("Product Changed", StrMess, 0, LngCase, 1)

            StrMess = "Case number " & LngCase & vbCrLf & StrMess

            Call (New TasksEvents).GblFunc_Add_SysEvent("Case Product Changed", StrMess)

            Me.Close()
            ' Error handling block added by VBA Code Commenter and Error Handler Add-In. DO NOT EDIT this block of code.

        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminChangeProduct-Btn_Change_Click")
        End Try
    End Sub

    Private Sub Btn_Clear_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Btn_Clear.Click
        Try
            Me.Txt_CaseID.IsReadOnly = False
            Me.Txt_CaseID.Text = String.Empty
            Me.Txt_CaseID.Focus()

            Me.Lbl_Details.Content = String.Empty
            Me.Txt_CurrentProduct.Text = String.Empty
            Me.Txt_CurrentProduct.Visibility = Windows.Visibility.Hidden
            Me.Combo_Product.Visibility = Windows.Visibility.Hidden
            Me.Combo_ProductType.Visibility = Windows.Visibility.Hidden
            ' Me.Combo_Status.Visibility = Windows.Visibility.Hidden
            Me.Btn_Change.Visibility = Windows.Visibility.Hidden
            Me.Btn_Clear.Visibility = Windows.Visibility.Hidden
            Me.Label1.Visibility = Windows.Visibility.Hidden
            Me.Label11.Visibility = Windows.Visibility.Hidden
            Me.Label8.Visibility = Windows.Visibility.Hidden
            Me.ProductType_Label.Visibility = Windows.Visibility.Hidden
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminChangeProduct-Btn_Clear_Click")
        End Try
    End Sub

    Private Sub Combo_ProductType_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles Combo_ProductType.SelectionChanged
        Try

            Dim StrSQL As String

            StrSQL = "SELECT Product_ID, Product_Name"
            StrSQL = StrSQL & " FROM L_Products"
            StrSQL = StrSQL & " WHERE (Product_Type = " & Me.Combo_ProductType.SelectedValue & ")"
            StrSQL = StrSQL & " ORDER BY Product_Name"

            Dim dataitems As DataTable = CommonDataFunctions.GetSQLDatatable(StrSQL)
            With Me.Combo_Product
                .ItemsSource = CType(dataitems, IListSource).GetList
                .SelectedValuePath = "Product_ID"
                .DisplayMemberPath = "Product_Name"
            End With

            'Call GPL(StrSQL, "ChangeProduct_Products", Form_FMA_ChangeProduct.Combo_Product)

            ' Error handling block added by VBA Code Commenter and Error Handler Add-In. DO NOT EDIT this block of code.


        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminChangeProduct-Combo_ProductType_SelectionChanged")
        End Try
    End Sub

    Private Sub Txt_CaseID_LostKeyboardFocus(ByVal sender As Object, ByVal e As System.Windows.Input.KeyboardFocusChangedEventArgs) Handles Txt_CaseID.LostKeyboardFocus
        
    End Sub

    Private Sub SQL_ProductTypes()
        Try
            Dim STRSQL As String
            STRSQL = "Select ProductType_ID,ProductType_Name from L_Producttypes where producttype_stagediagramid=37 order by ProductType_Name ASC"
            Dim productTypes As DataTable = CommonDataFunctions.GetSQLDatatable(STRSQL)
            With Me.Combo_ProductType
                .ItemsSource = CType(productTypes, IListSource).GetList
                .SelectedValuePath = "ProductType_ID"
                .DisplayMemberPath = "ProductType_Name"
            End With
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminChangeProduct-SQL_ProductTypes")
        End Try
    End Sub

    Private Sub Btn_Close_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Btn_Close.Click
        Me.Close()
    End Sub

    Private Sub frmAdminChangeProduct_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
    End Sub
End Class
